﻿Public Class frmCase

    Private Sub frmCase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getHeaderGrid_MainCase()
        getHeaderGrid_Case()
        getMainCase()
        reset()
    End Sub

    Protected Sub getHeaderGrid_MainCase()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "សំណុំរឿង", "ប្រភេទរឿងក្តី"}
        Dim HeaderWidth As Integer() = {100, 100, 700, 500}
        Dim i As Integer
        dgvMainCase.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvMainCase.Columns(i).HeaderText = HeaderText(i)
            dgvMainCase.Columns(i).Width = HeaderWidth(i)
        Next
        dgvMainCase.Columns(1).Visible = False
    End Sub

    Public Sub getMainCase()
        getHeaderGrid_MainCase()
        dgvMainCase.Rows.Clear()
        Dim oMainCase As New cMainCase
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oMainCase.select_MainCase(frmMain._LawyerID)
        For Each dr In dt.Rows
            dgvMainCase.Rows.Add(i, dr(0), dr(1), dr(2))
            i += 1
        Next
    End Sub

    Protected Sub getHeaderGrid_Case()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "លេខសំណុំរឿង", "ស្ថាប័នតុលាការ", "លេខទូរស័ព្ទ", "តម្លៃសេវាកម្ម", "រឿងក្តីជោគជ័យ", "ថ្ងៃចាប់ផ្តើម"}
        Dim HeaderWidth As Integer() = {100, 100, 250, 250, 150, 200, 200, 150}
        Dim i As Integer
        dgvCase.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvCase.Columns(i).HeaderText = HeaderText(i)
            dgvCase.Columns(i).Width = HeaderWidth(i)
        Next
        dgvCase.Columns(1).Visible = False
        dgvCase.Columns(7).DefaultCellStyle.Format = "dd/MM/yyyy"
    End Sub

    Public Sub getCase_byMainCaseID(ByVal MainCaseID As Long)
        getHeaderGrid_Case()
        dgvCase.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oCase.select_Case_byMainCaseID(MainCaseID)
        For Each dr In dt.Rows
            dgvCase.Rows.Add(i, dr(0), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7))
            i += 1
        Next
    End Sub

    Protected Sub reset()
        txtSearch.Clear()
        txtSearch.Focus()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub dgvMainCase_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMainCase.CellEnter
        getCase_byMainCaseID(dgvMainCase.CurrentRow.Cells(1).Value)
    End Sub

    Private Sub ShowFormWithoutClose(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        frm.Show()
        frm.BringToFront()
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        frmCaseEntry.btnFinishCase.Enabled = False
        ShowFormWithoutClose(frmCaseEntry)
    End Sub

    Private Sub btnAddCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCase.Click
        If dgvCase.Rows.Count > 0 Then
            frmCaseEntry._MainCaseID_ = dgvMainCase.CurrentRow.Cells(1).Value
            'frmCaseEntry._CaseID_ = dgvCase.CurrentRow.Cells(1).Value
            frmCaseEntry._isAddCase = True
            frmCaseEntry._isViewCase = False
            frmCaseEntry.EnableMainCase()
            frmCaseEntry.btnFinishCase.Enabled = False
            ShowFormWithoutClose(frmCaseEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់បង្ហាញទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If dgvCase.Rows.Count > 0 Then
            frmCaseEntry._MainCaseID_ = dgvMainCase.CurrentRow.Cells(1).Value
            frmCaseEntry._CaseID_ = dgvCase.CurrentRow.Cells(1).Value
            frmCaseEntry._isAddCase = False
            frmCaseEntry._isViewCase = True
            ShowFormWithoutClose(frmCaseEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់បង្ហាញទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub dgvCase_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvCase.DoubleClick
        btnShow_Click(Nothing, Nothing)
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        getMainCase_byKeyword()
    End Sub

    Public Sub getMainCase_byKeyword()
        getHeaderGrid_MainCase()
        dgvMainCase.Rows.Clear()
        Dim oMainCase As New cMainCase
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oMainCase.select_MainCase_byKeyword(frmMain._LawyerID, txtSearch.Text)
        If dt.Rows.Count > 0 Then
            For Each dr In dt.Rows
                dgvMainCase.Rows.Add(i, dr(0), dr(1), dr(2))
                i += 1
            Next
        Else
            dgvCase.Rows.Clear()
        End If
    End Sub
End Class